using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace stsdev
{
    public partial class SelectSolutionType : Form
    {

        public SelectSolutionType()
        {
            InitializeComponent();
        }

        private void SelectSolutionType_Load(object sender, EventArgs e)
        {
            // load default folder path 
            RegistryKey defaultFolderKey = Application.UserAppDataRegistry.CreateSubKey("TargetFolder");
            object defaultFolder = defaultFolderKey.GetValue("");
            if (defaultFolder != null && Directory.Exists(defaultFolder.ToString()))
            {
                txtParentDirectory.Text = defaultFolder.ToString();
            }
            else
            {
                txtParentDirectory.Text = @"c:\";
            }
            // load default solution version type
            RegistryKey defaultSolutionVersionType = Application.UserAppDataRegistry.CreateSubKey("SolutionVersionType");
            object solutionVersionType = defaultSolutionVersionType.GetValue("");
            if (solutionVersionType != null)
            {
                switch (solutionVersionType.ToString())
                {
                    case "VS2005":
                        radVS2005.Checked = true;
                        break;
                    case "VS2008_30":
                        radVS2008_30.Checked = true;
                        break;
                    case "VS2008_35":
                        radVS2008_35.Checked = true;
                        break;
                }
                // update UI
                EnableCreateButton();
                // set txtSolutionName as control with focus
                txtSolutionName.Focus();
            }

            //load the New and Browse text into the signing key combo box
            cbSigningKey.Items.Insert(int.Parse(Properties.Resources.NewSigningKeyIndex), Properties.Resources.NewSigningKeyText);
            cbSigningKey.Items.Insert(int.Parse(Properties.Resources.BrowseSigningKeyIndex), Properties.Resources.BrowseSigningKeyText);

            // load previous signing key
            RegistryKey defaultSigningKeyFileKey = Application.UserAppDataRegistry.CreateSubKey("SigningKeyFile");
            object solutionSigningKeyFile = defaultSigningKeyFileKey.GetValue("");
            if (solutionSigningKeyFile != null && File.Exists(solutionSigningKeyFile.ToString()))
            {
                int cbSelectIndex = cbSigningKey.Items.Add(solutionSigningKeyFile.ToString());
                cbSigningKey.SelectedIndex = cbSelectIndex;
            }
            else
                cbSigningKey.Text = "";
        }

        public string GetSolutionName()
        {
            return txtSolutionName.Text;
        }

        public string GetParentDirectory()
        {
            return txtParentDirectory.Text;
        }

        public string GetSigningKeyFile()
        {
            return cbSigningKey.Text;
        }

        public ISolutionProvider GetSelectedProvider()
        {
            return (ISolutionProvider)lstSolutionTypes.SelectedItem;
        }

        public SolutionVersionTypeEnum GetSolutionVersionType()
        {
            if (radVS2008_30.Checked)
                return SolutionVersionTypeEnum.VS2008_30;
            if (radVS2008_35.Checked)
                return SolutionVersionTypeEnum.VS2008_35;
            else
                return SolutionVersionTypeEnum.VS2005;
        }

        public void InitializeSolutionProviders(IList<ISolutionProvider> SolutionProviders)
        {
            lstSolutionTypes.DisplayMember = "Title";
            foreach (ISolutionProvider SolutionProvider in SolutionProviders)
            {
                lstSolutionTypes.Items.Add(SolutionProvider);
            }
            lstSolutionTypes.SelectedIndex = 0;
        }

        private void cmdChangeParentDirectory_Click(object sender, EventArgs e)
        {
            dlgFolderBrowser.SelectedPath = txtParentDirectory.Text;
            DialogResult result = dlgFolderBrowser.ShowDialog();
            if (result == DialogResult.OK)
            {
                txtParentDirectory.Text = dlgFolderBrowser.SelectedPath;
                RegistryKey defaultFolderKey = Application.UserAppDataRegistry.CreateSubKey("TargetFolder");
                defaultFolderKey.SetValue("", dlgFolderBrowser.SelectedPath);

            }
        }

        private void SolutionVersionType_Changed(object sender, EventArgs e)
        {
            RegistryKey defaultSolutionVersionType = Application.UserAppDataRegistry.CreateSubKey("SolutionVersionType");
            if (radVS2005.Checked)
                defaultSolutionVersionType.SetValue("", SolutionVersionTypeEnum.VS2005.ToString());
            if (radVS2008_30.Checked)
                defaultSolutionVersionType.SetValue("", SolutionVersionTypeEnum.VS2008_30.ToString());
            if (radVS2008_35.Checked)
                defaultSolutionVersionType.SetValue("", SolutionVersionTypeEnum.VS2008_35.ToString());
        }

        private void txt_TextChanged(object sender, EventArgs e)
        {
            EnableCreateButton();
        }

        private void EnableCreateButton()
        {
            if ((txtSolutionName.Text.Length > 0) &&
                 (txtParentDirectory.Text.Length > 0) &&
                 (cbSigningKey.Text.Length > 0))
            {
                cmdCreateSolution.Enabled = true;
            }
            else
            {
                cmdCreateSolution.Enabled = false;
            }
        }

        private void HandleSigningKeyIndexChange(object sender, EventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            if (cb != null)
            {
                if (cb.SelectedIndex == int.Parse(Properties.Resources.NewSigningKeyIndex))
                {
                    dlgSaveFileDialog.Title = "Create a new signing key file";
                    dlgSaveFileDialog.DefaultExt = "snk";
                    dlgSaveFileDialog.Filter = "Signing Key Files (*.snk)|*.snk";

                    dlgSaveFileDialog.FileName = "NewSigningKeyFile.snk";

                    DialogResult result = dlgSaveFileDialog.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        string KeyFileName = dlgSaveFileDialog.FileName;
                        StrongNameKeyBuilder.CreateNew(KeyFileName);
                        int itemIndex = cbSigningKey.Items.Add(KeyFileName);

                        //We need to add the key file name to the solution somehow so we can add it to the project
                        //SolutionBuilder.KeyFileName = KeyFileName;

                        RegistryKey defaultFolderKey = Application.UserAppDataRegistry.CreateSubKey("SigningKeyFile");
                        defaultFolderKey.SetValue("", KeyFileName);

                        cb.SelectedIndex = itemIndex;
                    }
                    else
                        cb.SelectedText = "";
                }
                else if (cb.SelectedIndex == int.Parse(Properties.Resources.BrowseSigningKeyIndex))
                {
                    dlgOpenFileDialog.Title = "Select a signing key file";
                    dlgOpenFileDialog.DefaultExt = "snk";
                    dlgOpenFileDialog.Filter = "Signing Key Files (*.snk)|*.snk";

                    dlgOpenFileDialog.CheckFileExists = true;

                    if (!string.IsNullOrEmpty(txtParentDirectory.Text))
                        dlgOpenFileDialog.InitialDirectory = txtParentDirectory.Text;
                    else
                        dlgOpenFileDialog.InitialDirectory = @"c:\";


                    DialogResult result = dlgOpenFileDialog.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        string KeyFileName = dlgOpenFileDialog.FileName;
                        int itemIndex = -1;

                        foreach (object item in cb.Items)
                        {
                            if (item.ToString() == KeyFileName)
                            {
                                itemIndex = cb.Items.IndexOf(item);
                                break;
                            }
                        }

                        if (itemIndex == -1)
                            itemIndex = cbSigningKey.Items.Add(KeyFileName);

                        RegistryKey defaultFolderKey = Application.UserAppDataRegistry.CreateSubKey("SigningKeyFile");
                        defaultFolderKey.SetValue("", KeyFileName);

                        cbSigningKey.SelectedIndex = itemIndex;
                    }
                    else
                        cb.SelectedText = "";
                }
            }
            else
                throw new Exception("Sender was not the combo box!");

            EnableCreateButton();
        }
    }
}